package com.tgjorgoski.sidekicknotes.htmlview;

import java.util.ArrayList;
import java.util.List;

import javax.swing.text.*;
import javax.swing.text.html.*;

public class HTMLDocChanged extends HTMLDocument {
	
	public HTMLDocChanged(StyleSheet ss) {
		super(ss);
	}
	protected AbstractElement createDefaultRoot() {
		// grabs a write-lock for this initialization and
		// abandon it during initialization so in normal
		// operation we can detect an illegitimate attempt
		// to mutate attributes.
		writeLock();
		MutableAttributeSet a = new SimpleAttributeSet();
		a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.HTML);
		BlockElement html = new BlockElement(null, a.copyAttributes());
		a.removeAttributes(a);
		a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.BODY);
		BlockElement body = new BlockElement(html, a.copyAttributes());
		a.removeAttributes(a);
		a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.P);
		BlockElement paragraph = new BlockElement(body, a.copyAttributes());
		a.removeAttributes(a);
		a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.CONTENT);
		RunElement brk = new RunElement(paragraph, a, 0, 1);
		Element[] buff = new Element[1];
		buff[0] = brk;
		paragraph.replace(0, 0, buff);
		buff[0] = paragraph;
		body.replace(0, 0, buff);
		buff[0] = body;
		html.replace(0, 0, buff);
		writeUnlock();
		return html;
	}

	public List<Element> getParagraphs(int start, int end) {
		List<Element> paragraphs = new ArrayList<Element>();
		while (start<=end) {
			Element sElement = getParagraphElement(start);
			paragraphs.add(sElement);
			start = sElement.getEndOffset();
		}		
		return paragraphs;
	}
}
